package splug.cjk;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.ArrayList;
import splug.cjk.packet.FriendPacket;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

public class FriendTab1 extends ListActivity implements OnItemClickListener{
	
	private String id;
	private ArrayList<String> content = new ArrayList<String>();
	private ArrayAdapter<String> m_adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.friend_tab1);
		
		Bundle b = this.getIntent().getExtras();
		id = b.getString("ID");
		
		ListView lv = getListView();
		
		ImageButton btn_add = (ImageButton) findViewById(R.id.btn_add_friend);
		btn_add.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(FriendTab1.this, FriendAdd.class);
				Bundle b = new Bundle();
				b.putString("ID", id);
				intent.putExtras(b);
				startActivity(intent);
			}
		});
		
		/*
		 * 친구 목록을 받아온다
		 */
		try {
			Socket socket = new Socket(LetsGoActivity.server_ip, 2007);
			
			InputStream is = socket.getInputStream();
			OutputStream os = socket.getOutputStream();
			
			OutputStreamWriter osw = new OutputStreamWriter(os);
			InputStreamReader isr = new InputStreamReader(is);
			
			osw.write(13);
			osw.flush();
			
			int ack = isr.read();
			
			if(ack == '1'){
				FriendPacket packet = new FriendPacket(null, id, "");
				ObjectOutputStream output = new ObjectOutputStream(os);
				output.writeObject(packet);
				
				ObjectInputStream input = new ObjectInputStream(is);
				packet = (FriendPacket) input.readObject();
				
				content = packet.getFriend_list();
			}
			else{
				Toast.makeText(FriendTab1.this, "네트워크 연결상대가 좋지 않습니다", Toast.LENGTH_SHORT).show();
			}
			
			
		} catch (Exception e) {
			Toast.makeText(FriendTab1.this, "네트워크 연결상대가 좋지 않습니다", Toast.LENGTH_SHORT).show();
		}
		
		//어댑터 추가
		m_adapter = new ArrayAdapter<String>(FriendTab1.this, android.R.layout.simple_list_item_1, content);
		lv.setAdapter(m_adapter);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * 리스트의 아이템이 클릭되었을 때의 이벤트
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long a) {
		super.onListItemClick(l, v, position, a);
		
		DialogRadio(position);
	}
	
	/**
	 * 쪽지보내기 다이얼로그
	 */
	private void DialogRadio(int position){
		final int pos = position;
		final CharSequence[] PhoneModels = {"쪽지보내기"};
		AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
		alt_bld.setTitle("쪽지보내기");
		alt_bld.setSingleChoiceItems(PhoneModels, -1, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				dialog.cancel();
				Intent intent = new Intent(FriendTab1.this, MessageTab2.class);
				Bundle b = new Bundle();
				b.putString("ID", id);
				b.putString("RECEIVER", content.get(pos));
				intent.putExtras(b);
				startActivity(intent);
			}
		});
		AlertDialog alert = alt_bld.create();
		alert.show();
	}

}
